package cn.xinfei.xdecision.datasource.mongodb;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xiaomifeng1010
 * @version 1.0
 * @date: 2022/6/23 18:39
 * @Description 本系统mongodb数据源配置
 */
@Configuration
@EnableMongoRepositories(mongoTemplateRef = "mongoTemplate")
@ConditionalOnProperty(name = "spring.data.mongo.data.enable")
public class PrimaryMongoTemplate {


    @Bean(name = "dataMongoProperties")
    @Primary
    @ConfigurationProperties(prefix = "spring.data.mongo.data")
    @ConditionalOnProperty(name = "spring.data.mongo.data.enable")
    public MongoProperties dataMongoProperties() {
        return new MongoProperties();
    }


    @Bean
    @Primary
    @ConditionalOnProperty(name = "spring.data.mongo.data.enable")
    public MongoTemplate mongoTemplate(@Qualifier("dataMongoProperties") MongoProperties dataMongoProperties) {
        MongoTemplate mongoTemplate = new MongoTemplate(mongoDatabaseFactory(dataMongoProperties));
        MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
        mongoMapping.setCustomConversions(mongoCustomConversions());
        mongoMapping.afterPropertiesSet();
        return mongoTemplate;
    }

    @Bean(name = "dataMongoFactory")
    @Primary
    @ConditionalOnProperty(name = "spring.data.mongo.data.enable")
    public MongoDatabaseFactory mongoDatabaseFactory(@Qualifier("dataMongoProperties") MongoProperties dataMongoProperties) {
        return new SimpleMongoClientDatabaseFactory(dataMongoProperties.getUri());
    }

    /**
     * mongoCustomConversions会由spring进行管理,
     * 按照加入的转换器,在数据库读写时对数据类型进行转换
     *
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "spring.data.mongo.data.enable")
    public MongoCustomConversions mongoCustomConversions() {
        List<Converter<?, ?>> converterList = new ArrayList<>();
        converterList.add(new BigDecimalToDecimal128Converter());
        converterList.add(new Decimal128ToBigDecimalConverter());
        return new MongoCustomConversions(converterList);
    }

}